package com.ruoyi.web.sso.service.impl;

import java.util.List;
import java.util.prefs.BackingStoreException;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.sso.common.util.AssertUtil;
import com.ruoyi.web.sso.common.util.BusinessException;
import com.ruoyi.web.sso.common.util.ErrorCode;
import com.ruoyi.web.sso.domain.Role;
import com.ruoyi.web.sso.domain.Staff;
import com.ruoyi.web.sso.domain.StaffRole;
import com.ruoyi.web.sso.mapper.RoleMapper;
import com.ruoyi.web.sso.mapper.StaffMapper;
import com.ruoyi.web.sso.mapper.StaffRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.sso.mapper.AreaStaffMapper;
import com.ruoyi.web.sso.domain.AreaStaff;
import com.ruoyi.web.sso.service.IAreaStaffService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 园所用户关联Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-11-03
 */
@Service
public class AreaStaffServiceImpl implements IAreaStaffService 
{
    @Autowired
    private AreaStaffMapper areaStaffMapper;

    @Autowired
    private StaffMapper staffMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private StaffRoleMapper staffRoleMapper;
    /**
     * 查询园所用户关联
     * 
     * @param areaId 园所用户关联主键
     * @return 园所用户关联
     */
    @Override
    public AreaStaff selectAreaStaffByAreaId(String areaId)
    {
        return areaStaffMapper.selectAreaStaffByAreaId(areaId);
    }

    /**
     * 查询园所用户关联列表
     * 
     * @param areaStaff 园所用户关联
     * @return 园所用户关联
     */
    @Override
    public List<AreaStaff> selectAreaStaffList(AreaStaff areaStaff)
    {
        return areaStaffMapper.selectAreaStaffList(areaStaff);
    }

    /**
     * 新增园所用户关联
     * 
     * @param areaStaff 园所用户关联
     * @return 结果
     */
    @Override
    public int insertAreaStaff(AreaStaff areaStaff)
    {
        return areaStaffMapper.insertAreaStaff(areaStaff);
    }

    /**
     * 修改园所用户关联
     * 
     * @param areaStaff 园所用户关联
     * @return 结果
     */
    @Override
    public int updateAreaStaff(AreaStaff areaStaff)
    {
        return areaStaffMapper.updateAreaStaff(areaStaff);
    }

    /**
     * 批量删除园所用户关联
     * 
     * @param areaIds 需要删除的园所用户关联主键
     * @return 结果
     */
    @Override
    public int deleteAreaStaffByAreaIds(String areaIds)
    {
        return areaStaffMapper.deleteAreaStaffByAreaIds(Convert.toStrArray(areaIds));
    }

    /**
     * 删除园所用户关联信息
     * 
     * @param areaId 园所用户关联主键
     * @return 结果
     */
    @Override
    public int deleteAreaStaffByAreaId(String areaId)
    {
        return areaStaffMapper.deleteAreaStaffByAreaId(areaId);
    }

    @Override
    @Transactional
    public AjaxResult deleteStaffAreaInfo(String areaId, String staffId) {
        areaStaffMapper.deleteStaffAreaInfo(new AreaStaff(areaId,staffId));
        Staff staff = staffMapper.selectStaffById(staffId);
        if (AssertUtil.isNotEmpty(staff) ){
            if (StringUtils.equals(areaId,staff.getAreaId())){
                staff.setAreaId("0");
                String areaId1  = areaStaffMapper.getAreaId(staffId);
                if (AssertUtil.isNotEmpty(staff) ){
                    staff.setAreaId(areaId1);
                }else {
                    throw new BusinessException(ErrorCode.UNDEFINED, "无默认园区");
                }
                staffMapper.updateStaff(staff);
            }
        }else {
            return AjaxResult.error("用户不存在");
        }
        return AjaxResult.success("删除成功");
    }

    @Override
    public AjaxResult deleteStaffAreaInfos(String areaId, String[] staffIds) {
        areaStaffMapper.deleteStaffAreaInfos(areaId,staffIds);
        return AjaxResult.success("删除成功");
    }

    @Override
    public AjaxResult setAreaDefault(String areaId, String staffId) {
        Staff staff = staffMapper.selectStaffById(staffId);
        if (AssertUtil.isNotEmpty(staff)){
            staff.setAreaId(areaId);
            List<AreaStaff> list = areaStaffMapper.selectAreaStaffList(new AreaStaff(areaId,staff.getId()));
            if (list.size()>0 ){
                List<StaffRole> list1 = staffRoleMapper.getAreaRoleList(areaId,staffId);
                if (list1.size()>0 ){
                    AreaStaff areaStaff = list.get(0);
                    areaStaff.setRoleId(list1.get(0).getRoleId());
                    areaStaffMapper.updateAreaStaff(areaStaff);
                }
            }

            staffMapper.updateStaff(staff);
            return AjaxResult.success("设置成功");
        }else {
            return AjaxResult.error("用户不存在");
        }
    }

    @Override
    public List<AreaStaff> selectInAreaId(List<String> areaIds) {
        return areaStaffMapper.selectInAreaId(areaIds.toArray(new String[areaIds.size()]));
    }


    @Override
    public List<AreaStaff> selectInAreaIdAndRoleId(String areaIds,String roleId) {
        return areaStaffMapper.selectInAreaIdAndRoleId(areaIds,roleId);
    }
}
